package com.chang.product.app;

import com.chang.common.utils.PageUtils;
import com.chang.common.utils.R;
import com.chang.product.dto.AttrResponseVo;
import com.chang.product.entity.ProductAttrValueEntity;
import com.chang.product.service.AttrService;
import com.chang.product.service.ProductAttrValueService;
import com.chang.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author chang
 * @email 2045807586@qq.com
 * @date 2024-01-02 16:10:53
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    // 获取spu规格
    @GetMapping("/base/listforspu/{spuId}")
    public R getSpuInfo(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.baseAttrListForSpuInfo(spuId);

        return R.ok().put("data", productAttrValueEntities);
    }

    // /product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    public R updateSpuInfo(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> attrValueEntityList) {
        productAttrValueService.updateBaseAttrSpuInfo(spuId, attrValueEntityList);

        return R.ok();
    }
    // 根据不同的路径参数,来判断查询销售属性(sale/list)还是基本属性(base/list)
    @GetMapping("{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String attrType) {
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrResponseVo attrResponseVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrResponseVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrvo) {
        attrService.saveAttr(attrvo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
